package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService attrValueService;
    @Autowired
    private RoomFacilityService facilityService;
    @Autowired
    private RoomLabelService labelService;
    @Autowired
    private RoomPaymentTypeService paymentTypeService;
    @Autowired
    private RoomLeaseTermService leaseTermService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    @Transactional

    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        /*roominfo的saveorUpdate*/
        boolean isUpdate = roomSubmitVo.getId() != null;
        this.saveOrUpdate(roomSubmitVo);
        /*删除*/
        if (isUpdate) {
            /*图片*/
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                    .eq(GraphInfo::getItemId, roomSubmitVo.getId())
                    .eq(GraphInfo::getItemType, ItemType.ROOM));
            /*属性信息*/
            attrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>()
                    .eq(RoomAttrValue::getRoomId, roomSubmitVo.getId()));
            /*配套信息*/
            facilityService.remove(new LambdaQueryWrapper<RoomFacility>()
                    .eq(RoomFacility::getRoomId, roomSubmitVo.getId()));
            /*标签信息*/
            labelService.remove(new LambdaQueryWrapper<RoomLabel>()
                    .eq(RoomLabel::getRoomId, roomSubmitVo.getId()));
            /*支付方式*/
            paymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>()
                    .eq(RoomPaymentType::getRoomId, roomSubmitVo.getId()));
            /*可选租期*/
            leaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>()
                    .eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId()));

            /*删除redis缓存中的信息，确保数据一致性*/
            redisTemplate.delete(RedisConstant.APP_ROOM_PREFIX + roomSubmitVo.getId());
        }
        Long roomSubmitVoId = roomSubmitVo.getId();
        /*图片*/
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        graphInfoService.insertByGraphVoAndItem(ItemType.ROOM, roomSubmitVoId, graphVoList);
        /*属性信息*/
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        attrValueService.insertByItemIdAndIds(roomSubmitVoId, attrValueIds);
        /*配套信息*/
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        facilityService.insertByItemIdAndIds(roomSubmitVoId, facilityInfoIds);
        /*标签信息*/
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        labelService.insertByItemIdAndIds(roomSubmitVoId, labelInfoIds);
        /*支付方式*/
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        paymentTypeService.insertByItemIdAndIds(roomSubmitVoId, paymentTypeIds);
        /*可选租期*/
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        leaseTermService.insertByItemIdAndIds(roomSubmitVoId, leaseTermIds);
    }


    @Override
    public IPage<RoomItemVo> pageList(Page<RoomItemVo> roomItemVoIPage, RoomQueryVo queryVo) {
        IPage<RoomItemVo> pageList = roomInfoMapper.pageList(roomItemVoIPage, queryVo);
        return pageList;
    }


    @Override
    public RoomDetailVo getInfo(Long id) {
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        if (roomInfo == null) return null;

        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        List<GraphVo> infoByItem = graphInfoMapper.getInfoByItem(ItemType.ROOM, id);
        List<AttrValueVo> attrValueVos = attrValueMapper.selectListByRoomId(id);

        //5.查询facilityInfoList
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getInfoByItemId(id);

        //6.查询labelInfoList
        List<LabelInfo> labelInfoList = labelInfoMapper.getLabelInfo(id);

        //7.查询paymentTypeList
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);

        //8.查询leaseTermList
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);


        RoomDetailVo adminRoomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, adminRoomDetailVo);

        adminRoomDetailVo.setApartmentInfo(apartmentInfo);
        adminRoomDetailVo.setGraphVoList(infoByItem);
        adminRoomDetailVo.setAttrValueVoList(attrValueVos);
        adminRoomDetailVo.setFacilityInfoList(facilityInfoList);
        adminRoomDetailVo.setLabelInfoList(labelInfoList);
        adminRoomDetailVo.setPaymentTypeList(paymentTypeList);
        adminRoomDetailVo.setLeaseTermList(leaseTermList);

        return adminRoomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {
        super.removeById(id);

        /*图片*/
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                .eq(GraphInfo::getItemId, id)
                .eq(GraphInfo::getItemType, ItemType.ROOM));
        /*属性信息*/
        attrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>()
                .eq(RoomAttrValue::getRoomId, id));
        /*配套信息*/
        facilityService.remove(new LambdaQueryWrapper<RoomFacility>()
                .eq(RoomFacility::getRoomId, id));
        /*标签信息*/
        labelService.remove(new LambdaQueryWrapper<RoomLabel>()
                .eq(RoomLabel::getRoomId, id));
        /*支付方式*/
        paymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>()
                .eq(RoomPaymentType::getRoomId, id));
        /*可选租期*/
        leaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>()
                .eq(RoomLeaseTerm::getRoomId, id));

        /*删除redis缓存中的信息，确保数据一致性*/
        redisTemplate.delete(RedisConstant.APP_ROOM_PREFIX + id);
    }
}




